package com.onstar.cn.weight.anim;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.onstar.cn.weight.R;

/**
 * Created by 6335yz on 2016/8/22.
 */
public class HomeLoadingLayout extends LinearLayout {

    private ImageView[] imgs;
    private int imgPos = 0;
    private View view;
    private boolean isAnimRunning;
    private AnimatorSet mAnimatorSet = new AnimatorSet();

    public HomeLoadingLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(attrs);
    }

    public HomeLoadingLayout(Context context) {
        super(context);
        initView(null);
    }


    public void startAnim() {
//        if (isAnimRunning) {
//            return;
//        } else {
//            stopAnim();
//            handler.post(runnable);
//        }
        setAllAnimationsRepeatCount(ValueAnimator.INFINITE);
        mAnimatorSet.start();
    }

//    Runnable runnable = new Runnable() {
//        public void run() {
//            isAnimRunning = true;
//            if (imgPos > 3) {
//                imgPos = 0;
//            }
//            handler.sendEmptyMessage(imgPos);
//            handler.postDelayed(runnable, 300);
//            imgPos++;
//        }
//    };

    public void stopAnim() {
//        handler.removeCallbacksAndMessages(null);
        isAnimRunning = false;
        for (ImageView iv : imgs) {
            iv.setImageResource(R.drawable.home_loading_indicate);
        }
        imgPos = 0;
    }

//    Handler handler = new Handler() {
//        @Override
//        public void handleMessage(Message msg) {
//            super.handleMessage(msg);
//            if (msg.what == 0) {
//                imgs[3].setImageResource(R.drawable.home_loading_small);
//                imgs[imgPos - 1].setImageResource(R.drawable.home_loading_big);
//            } else {
//                imgs[imgPos - 2].setImageResource(R.drawable.home_loading_small);
//                imgs[imgPos - 1].setImageResource(R.drawable.home_loading_big);
//            }
//        }
//    };

    private void initView(AttributeSet attrs) {
        view = inflate(getContext(), R.layout.home_loading, this);
        ImageView iv1 = (ImageView) findViewById(R.id.one_iv);
        ImageView iv2 = (ImageView) findViewById(R.id.two_iv);
        ImageView iv3 = (ImageView) findViewById(R.id.three_iv);
//        ImageView iv4 = (ImageView) findViewById(R.id.four_iv);
        imgs = new ImageView[]{iv1, iv2, iv3};
//		startAnim();
        int period = 1000;

        ObjectAnimator dotOneJumpAnimator = createDotJumpAnimator(iv1, 0);
        dotOneJumpAnimator.addUpdateListener(new InvalidateViewOnUpdate(this));

        mAnimatorSet.playTogether(dotOneJumpAnimator, createDotJumpAnimator(iv2,
                period / 6), createDotJumpAnimator(iv3, period * 2 / 6));
    }

    private void setAllAnimationsRepeatCount(int repeatCount) {
        for (Animator animator : mAnimatorSet.getChildAnimations()) {
            if (animator instanceof ObjectAnimator) {
                ((ObjectAnimator) animator).setRepeatCount(repeatCount);
            }
        }
    }

    private ObjectAnimator createDotJumpAnimator(ImageView JumpTextSpan, long delay) {
        ObjectAnimator jumpAnimator = ObjectAnimator.ofFloat(JumpTextSpan, "translationY", 0, -60);
        jumpAnimator.setEvaluator(new SinTypeEvaluator());
        jumpAnimator.setDuration(1000);
        jumpAnimator.setStartDelay(delay);
        jumpAnimator.setRepeatCount(ValueAnimator.INFINITE);
        jumpAnimator.setRepeatMode(ValueAnimator.RESTART);
        return jumpAnimator;
    }
}
